<template>
  <div class="login-container">
    <div class="login-card">
      <div class="logo-container">

      </div>
      <h2 class="login-title">欢迎登录</h2>
      <el-form @submit.prevent="handleLogin">
        <el-form-item>
          <el-input 
            v-model="form.username" 
            placeholder="请输入账号"
            class="custom-input"
            :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            show-password
            class="custom-input"
            :prefix-icon="Lock"
          />
        </el-form-item>
        <el-button 
          type="primary" 
          native-type="submit"
          class="submit-btn"
          :loading="loading"
        >
          {{ loading ? '登录中...' : '立即登录' }}
        </el-button>
      </el-form>
      <div class="footer-links">
        <span @click="goRegister" >立即注册</span>
        <span @click="goEmailLogin" >邮箱登录</span>
        <span @click="forgotPassword">忘记密码？</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/modules/auth' // 添加这行导入
import axios from 'axios'

const router = useRouter()
const form = ref({
  username: '',
  password: ''
})
const loading = ref(false)




const handleLogin = async () => {
  loading.value = true;
  const authStore = useAuthStore(); // 在这里获取 authStore 实例
  try {
    const result = await authStore.login({
      nickname: form.value.username,
      password: form.value.password
    });
    
    if (result?.code === 200) {
      router.push({ name: 'conversation' });
    }
  } catch (error) {
    console.error('登录失败:', error);
    ElMessage.error(error.message || '登录失败');
  } finally {
    loading.value = false;
  }
}

const goRegister = () => {
  // 改为使用query参数控制显示注册界面
  router.push({ 
    name: 'email-login',
    query: { showRegister: 'true' }
  })
}
const goEmailLogin = () => {
  // 改为使用query参数控制显示注册界面
  router.push({ 
    name: 'email-login',
    query: { showRegister: 'false' }
  })
}
</script>


<style lang="scss" scoped>
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(145deg, #f0f2f5 0%, #dfe3e8 100%);
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: -10%;
    left: -10%;
    right: -10%;
    bottom: -10%;
    background: inherit;
    z-index: -1;
  }

  .login-card {
    width: 100%;
    max-width: 440px;
    padding: 40px;
    background: rgba(255, 255, 255, 0.98);
    border-radius: 20px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.08);
    position: relative;
    margin: 20px;

    .logo-container {
      text-align: center;
      margin-bottom: 30px;
      
      .logo {
        width: 80px;
        height: 80px;
        margin-bottom: 20px;
      }
    }

    .login-title {
      text-align: center;
      color: #2c3e50;
      margin: 0 0 35px;
      font-size: 26px;
      font-weight: 600;
      letter-spacing: 1px;
    }

    .custom-input {
      margin-bottom: 25px;

      :deep(.el-input__wrapper) {
        padding: 14px 16px;
        border-radius: 12px;
        background: rgba(245, 247, 250, 0.8);
        transition: all 0.3s ease;

        &:hover {
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
        }
      }

      :deep(.el-input__prefix) {
        margin-right: 8px;
      }
    }

    .submit-btn {
      width: 100%;
      height: 48px;
      font-size: 16px;
      border-radius: 12px;
      margin-top: 15px;
      background: linear-gradient(135deg, #4a90e2 0%, #3065b5 100%);
      border: none;
      transition: all 0.3s ease;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 16px rgba(74, 144, 226, 0.3);
      }
    }

    .footer-links {
      margin-top: 25px;
      display: flex;
      justify-content: space-between;
      color: #4a90e2;
      font-size: 14px;

      span {
        cursor: pointer;
        transition: color 0.3s ease;

        &:hover {
          color: #3065b5;
          text-decoration: underline;
        }
      }
    }
  }
}
</style>